package filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.annotation.WebInitParam;

import java.io.IOException;

/**
 * ClassName: CharsetFilter
 * Package: ying.filter02
 * Description:
 *
 * @Author 刘英杰
 * @Create 2024/10/6 15:30
 * @Version 1.0
 */

@WebFilter(filterName = "charsetFilter",urlPatterns = "/user/*",initParams = @WebInitParam(name = "charset",value = "UTF-8"))
public class CharsetFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//        request.setCharacterEncoding("UTF-8"); //可选

        FilterRegistration charsetFilter = request.getServletContext().getFilterRegistration("charsetFilter");
        String charset = charsetFilter.getInitParameter("charset");

        response.setContentType("application/json;charset=" + charset);
        chain.doFilter(request, response);
    }
}
